#ifndef classMODBUSH
 #define classMODBUSH


 #include "CPU_const.h"
 

class MODBUS
{
  bool Gamma;
  char myID, objectID, queryID, InCmnd, multiplicity;

  
  UARTbuf *pUx;
  GOST28147 *pGOST;
uint InBgn, InEnd, InID;
  
  uint posa, NofByte;

public:
  
  char OutCmnd;
  usht OutAdr, OutNregs, OutData1, OutData2;
  
private:  
  bool findID(void);
  uint readShort(void);
  void writeShort(uint data);
  void writeLong(uint data);
  uint RegsToBytes(uint adr,uint nBytes);
  
  
  uint dataRead(uint adr, uint n_bytes);
  uint writeSinglReg(uint adr, uint data);
  uint writeMultReg(uint adr, uint Nreg);
  uint writeMaskReg(uint adr, uint mskAND, uint mskOR);
  void writeData(usht *pData, uint Nregs);
  
  void formAnswer(void);
  
  
  
public:
//
  MODBUS(UARTbuf *pUART, char my_id, char object_id);
  
//

  void SetMultiplicity(char val);
  char GetMultiplicity(void);
  
  void SetGamma(bool GammEncryptOn);
  void EncryptSetUp(GOST28147 *pG, bool GammEncryptOn);
  
  void check_Query(void);
  uint formAsk(char *pData, uint GammaLo, uint GammaHi);
//uint formAsk(char *pData);
  void XORask(uint indx);
//
};












/****************************************************************************************************/
/*                              L O W L E V E L     D E F I N E                                     */
/*                               ( d o n ' t     c h a n g e )                                      */
/****************************************************************************************************/
 #define R_H_R            3           //MODBUS: Read Holding Registers
 #define P_S_R            6           //MODBUS: Preset Single Register
 #define P_M_R            16          //MODBUS: Preset Multiple Registers
 #define M_W_R            22          //MODBUS: Mask Write 4x Register
 #define ERRmsk           0x80        //MODBUS: Mask Write 4x Register

 #define ILL_FUNC         1           //MODBUS ERROR: Illegal Function
 #define ILL_ADDR         2           //MODBUS ERROR: Illegal Data Address
 #define ILL_VAL          3           //MODBUS ERROR: Illegal Data Value
 #define SLV_FAIL         4           //MODBUS ERROR: Slave Device Failure
 #define ACKNLEDG         5           //MODBUS ERROR: Acknowledge
 #define SLV_BUSY         6           //MODBUS ERROR: Slave Device Busy
 #define NEG_ACKN         7           //MODBUS ERROR: Negative Acknowledge
 #define MEM_ERR          8           //MODBUS ERROR: Memory Parity Error
 #define No_ERR           255         //MODBUS ERROR: No_error

/*      Q U E R Y      C H E C K I N G      C A S E S                */
 #define IDseek           0     //CASE: seeking ID in input buffer
 #define COMask           1     //CASE: seeking ask COMMAND in input buffer
 #define COMansw          2     //CASE: seeking answer COMMAND in input buffer
 #define NoBwait          3     //CASE: waiting number of bytes in PMR command
 #define ENDwait          4     //CASE: waiting the end of query
 #define QUERYok          255   //CASE: waiting the end of query



 #define LastAdr          65536 //
 #define MaxNregs         127         //MODBUS: MAX number of registers in query/answer
 #define MaxNbytes       ( MaxNregs<<1)
/****************************************************************************************************/
/****************************************************************************************************/
/****************************************************************************************************/
#endif